{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## [java 转型问题](https://www.runoob.com/w3cnote/java-transformation-problem.html)\n",
    "为什么需要java转型?\n",
    "\n",
    "这是多态的体现， 父类强转成子类对象后，调用子类的方法。\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Java 转型问题其实并不复杂，只要记住一句话：父类引用指向子类对象。\n",
    "\n",
    "什么叫父类引用指向子类对象，且听我慢慢道来。\n",
    "\n",
    "从 2 个名词开始说起：**向上转型(upcasting)** 、**向下转型(downcasting)**。\n",
    "\n",
    "举个例子：有2个类，Father 是父类，Son 类继承自 Father。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第 1 个例子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-31T11:14+0000",
     "start_time": "2020-10-31T11:14:27.736Z"
    }
   },
   "outputs": [],
   "source": [
    "Father f1 = new Son();   // 这就叫 upcasting （向上转型)\n",
    "// 现在 f1 引用指向一个Son对象\n",
    "\n",
    "Son s1 = (Son)f1;   // 这就叫 downcasting (向下转型)\n",
    "// 现在f1 还是指向 Son对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第 2 个例子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Father f2 = new Father();\n",
    "Son s2 = (Son)f2;       // 出错，子类引用不能指向父类对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "你或许会问，第1个例子中：Son s1 = (Son)f1; 问为什么是正确的呢。\n",
    "\n",
    "很简单因为 f1 指向一个子类对象，`Father f1 = new Son();` 子类 s1 引用当然可以指向子类对象了。\n",
    "\n",
    "而 f2 被传给了一个 Father 对象，`Father f2 = new Father();` 子类 s2 引用不能指向父类对象。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 总结：\n",
    "\n",
    "1、父类引用指向子类对象，而子类引用不能指向父类对象。\n",
    "\n",
    "2、把子类对象直接赋给父类引用叫upcasting向上转型，向上转型不用强制转换吗，如：\n",
    "\n",
    "```java\n",
    "Father f1 = new Son();\n",
    "```\n",
    "\n",
    "3、把指向子类对象的父类引用赋给子类引用叫向下转型(downcasting)，要强制转换，如：\n",
    "\n",
    "f1 就是一个指向子类对象的父类引用。把f1赋给子类引用 s1 即 `Son s1 = (Son)f1;`\n",
    "\n",
    "其中 f1 前面的(Son)必须加上，进行强制转换。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Java",
   "language": "java",
   "name": "java"
  },
  "language_info": {
   "codemirror_mode": "java",
   "file_extension": ".jshell",
   "mimetype": "text/x-java-source",
   "name": "Java",
   "pygments_lexer": "java",
   "version": "11.0.8+10-post-Ubuntu-0ubuntu120.04"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
